package com.core.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.core.common.controller.BaseController;
import com.core.common.dao.Pager;
import com.core.common.pageModel.Combo;
import com.core.common.pageModel.EasyuiDataGridJson;
import com.core.common.pageModel.JsonMessage;
import com.core.sys.service.IUserService;
import com.core.sys.vo.UserVo;

@Controller
@RequestMapping(value = "/admin/sys/user")
public class UserController extends BaseController{

	@Resource
	private IUserService userService;
	
	// 返回用户管理主页
	@RequestMapping()
	public String toIndex() {
		return "redirect:/admin/sys/user/index";
	}
	@RequestMapping("index")
	public String index() {
		return "admin/sys/user/index";
	}
	
	//获取用户列表
	@RequestMapping(value = "/getList", method = RequestMethod.POST)
	@ResponseBody
//	public EasyuiDataGridJson getUserList(int page, int rows,String sort, String order, HttpServletRequest request) {
	public EasyuiDataGridJson getUserList(HttpServletRequest request) {    
		EasyuiDataGridJson json = new EasyuiDataGridJson();
		String userName = request.getParameter("name");
		String createdateStart = request.getParameter("createdatetimeStart");
		String createdateEnd = request.getParameter("createdatetimeEnd");
		String updatedateStart = request.getParameter("updatedatetimeStart");
		String updatedateEnd = request.getParameter("updatedatetimeEnd");
		String orgIds = request.getParameter("orgIds");
		String[] deptIds = null;
		if (StringUtils.isNotBlank(orgIds)) {
			deptIds = orgIds.split(",");
		}
		Pager<UserVo> pager = userService.getUserList(userName,createdateStart,
				createdateEnd, updatedateStart, updatedateEnd,deptIds);
		if(pager != null){
			json.setTotal(pager.getTotalCount());
			json.setRows(pager.getDatas());			
		}
		if(CollectionUtils.isEmpty(json.getRows())){
		    json.setTotal(0l);
            json.setRows(new ArrayList<UserVo>());
		}
		return json;			
	}
	
	// 创建用户
	@RequestMapping(value = "/createUser")
	@ResponseBody
	public JsonMessage createUser(UserVo user,HttpServletRequest request,HttpServletResponse response) {
		JsonMessage jsonMsg = new JsonMessage();
		if (userService.existUserName(user)) {
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("用户名已存在！");
			jsonMsg.setObj("用户名已存在！");
			return jsonMsg;
		} 
		try {
			String currentUserName = getCurrentUserName(request);
			user.setCreateUser(currentUserName);
			userService.save(user);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("添加用户出错！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("添加用户成功！");
		
		return jsonMsg;
	}

	// 返回编辑用户数据
	@RequestMapping(value = "/editUserForm")
	@ResponseBody
	public UserVo editUserForm(long userId) {
		UserVo user = userService.getUserByUserId(userId);
		user.setPassWord("");
		return user;
	}
	
	// 编辑用户
	@RequestMapping(value = "/editUser", method = RequestMethod.POST)
	@ResponseBody
	public JsonMessage editUser(UserVo user, HttpServletRequest request, HttpServletResponse response) {
		JsonMessage jsonMsg = new JsonMessage();
		if (userService.existUserName(user)) {
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("用户名已存在！");
			jsonMsg.setObj("用户名已存在！");
			return jsonMsg;
		} 
		try {
			String currentUserName = getCurrentUserName(request);
			user.setUpdateUser(currentUserName);
			userService.update(user);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("编辑用户失败！");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("编辑用户成功！");
		return jsonMsg;
	}
	
	// 批量删除用户
	@RequestMapping(value = "/deleteUser")
	@ResponseBody
	public JsonMessage deleteUser(HttpServletRequest request, String userIds) {
		String[] userIdArray = userIds.split(",");
		JsonMessage jsonMsg = new JsonMessage();
		try {
			userService.delete(userIdArray);
		} catch (Exception e) {
			e.printStackTrace();
			jsonMsg.setSuccess(false);
			jsonMsg.setMsg("删除用户失败!");
			jsonMsg.setObj(e.getMessage());
			return jsonMsg;
		}
		jsonMsg.setSuccess(true);
		jsonMsg.setMsg("删除用户成功!");
		return jsonMsg;
	}
	
	@RequestMapping(value = "/getUserComboList")
	@ResponseBody
	List<Combo> getUserComboList(long userId){
		return userService.getUserComboList(userId);
	}
}
